package cn.david.org.controller;


import cn.david.basic.util.AjaxResult;
import cn.david.basic.util.PageList;
import cn.david.org.domain.Department;
import cn.david.org.query.DepartmentQuery;
import cn.david.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
        System.out.println(department+"++++++++++++");
        try {
            if (department.getId()==null){
            departmentService.add(department);
            }else {
                departmentService.upadte(department);
            }
            return AjaxResult.me();

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("添加失败"+e.getMessage());
        }

    }
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id ){
        try {
            departmentService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败"+e.getMessage());
        }


    }
    @GetMapping("/{id}")
    public Department queryById(@PathVariable("id") Long id){
        return departmentService.getById(id);

    }
    @GetMapping
    public List<Department> getAll(){
        return departmentService.getAll();
    }
    @GetMapping("/deptTree")
    public List<Department> getDeptTree(){
        return departmentService.getDeptTree();
    }
    @PostMapping
    public PageList<Department> list(@RequestBody DepartmentQuery query){

        return departmentService.queryData(query);
    }
    @PatchMapping
    public AjaxResult deleteBys(@RequestBody Long[] ids){
        try {
            departmentService.deleteByIds(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败"+e.getMessage());
        }

    }


}
